package game.engine.network.cmd;

import json.JSONException;
import json.JSONObject;

public abstract class Command
{
	public static String SEPARATOR = ":";
	public static String KEY_IDCMD = "idcmd";
	public static String KEY_USERCODE = "uc";
	public static String KEY_USERID = "ukeyid";
	public static String POSITIONX = "posx";
	public static String POSITIONY = "posy";
	public static String IDOBJ = "idobj";

	protected JSONObject _object = null;

	public Command(JSONObject pObject)
	{
		_object = pObject;
	}

	public Command(EnumCommand pIdCmd)
	{
		_object = new JSONObject();
		addParam( KEY_IDCMD , pIdCmd.getValue() );
	}

	public String getUserCode()
	{
		return (String) getParam( KEY_USERCODE );
	}

	public Integer getIdCmd()
	{
		return (Integer) getParam( KEY_IDCMD );
	}

	public String getCmdString()
	{
		return _object.toString();
	}

	protected Object getParam( String pKey )
	{
		try
		{
			return _object.get( pKey );
		}
		catch ( JSONException e )
		{
			e.printStackTrace();
		}
		return null;
	}

	protected void addParam( String pKey , Object pObject )
	{
		try
		{
			_object.put( pKey , pObject );
		}
		catch ( JSONException e )
		{
			e.printStackTrace();
		}
	}

	public void setObject( JSONObject pObject )
	{
		_object = pObject;
	}
}
